package com.example.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mapper.WalletDetailMapper;
import com.example.mapper.WalletMapper;
import com.example.pojo.Wallet;
import com.example.pojo.WalletDetail;
import com.example.service.WalletService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class WalletServiceImpl extends ServiceImpl<WalletMapper, Wallet> implements WalletService {

    @Autowired
    private WalletMapper walletMapper;

    @Autowired
    private WalletDetailMapper walletDetailMapper;

    /**
     * 查询用户钱包余额
     * @param id 用户id
     * @return
     */
    @Override
    public Double checkWalletMoney(Long id) {
        LambdaQueryWrapper<Wallet> walletLambdaQueryWrapper = new LambdaQueryWrapper<>();
        walletLambdaQueryWrapper.eq(id != null,Wallet::getId,id);
        Wallet wallet = this.getOne(walletLambdaQueryWrapper);
        log.info("用户余额表数据：{}",wallet);
        return wallet.getMoney();
    }

    /**
     * 用户消费
     * @param id 用户id
     * @param consumeMoney  消费金额
     * @return
     */
    @Override
    @Transactional
    public String userConsume(Long id,Double consumeMoney) {
        //wallet表减去消费金额
        Wallet wallet = new Wallet();
        wallet.setId(id);
        //原来的金额
        Double money = checkWalletMoney(id);
        //减去销售的金额
        wallet.setMoney(money-consumeMoney);
        boolean result = this.update(wallet, null);

        if(result){
            //写入金额变动明细表
            WalletDetail walletDetail = new WalletDetail();
            walletDetail.setWalletId(id);
            walletDetail.setDetail("消费"+consumeMoney+",还剩"+(money-consumeMoney));
            walletDetailMapper.insert(walletDetail);
            return "消费成功";
        }
        return "消费失败";
    }

    /**
     * 用户退款
     * @param id 用户id
     * @param refundMoney  退款金额
     * @return
     */
    @Override
    @Transactional
    public String userRefund(Long id, Double refundMoney) {
        //wallet表加上退款金额
        Wallet wallet = new Wallet();
        wallet.setId(id);
        //原来的金额
        Double money = checkWalletMoney(id);
        //加上退款的金额
        wallet.setMoney(money+refundMoney);
        boolean result = this.update(wallet, null);
        if(result){
            //写入金额变动明细表
            WalletDetail walletDetail = new WalletDetail();
            walletDetail.setWalletId(id);
            walletDetail.setDetail("退款"+refundMoney+",还剩"+(money+refundMoney));
            walletDetailMapper.insert(walletDetail);
            return "退款成功";
        }
        return "退款失败";
    }

    /**
     * 查询用户钱包金额变动明细
     * @return
     */
    @Override
    public List<WalletDetail> moneyDetail() {
        List<WalletDetail> walletDetails = walletDetailMapper.selectList(null);
        return walletDetails;
    }


}
